package code.android.iMemorizer.activity;

import java.util.ArrayList;

import code.andorid.iMemorizer.Rotate3dAnimation;

import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class RootActivity extends ListActivity
{
	public static final String DEBUG_TAG = "MainActivity";
	private final int FLIP_VELOCITY = 700;
	
	ViewFlipper mViewFlipper = null;
	ArrayAdapter<String> mArrayAdapter = null;
	EditText mEditText = null;
	ViewGroup mContainer = null;
	TextView mTextView = null;
	ListView mListView = null;
	
	ArrayList<String> mWordList = new ArrayList<String>();
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setupViews();

        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, mWordList);
        setListAdapter(mArrayAdapter);
        
        Log.i(DEBUG_TAG, "OnCreate()");
 
        
        
       
    }
    
    private void sendText()
    {
		String text = mEditText.getText().toString();
		
		if(text.length() == 0)
		{
			return;
		}
		
		mArrayAdapter.add(text);
		mEditText.setText(null);
    }
    
    @Override
    protected void onPause() 
    {
    	Log.i(DEBUG_TAG, "OnPause()");
    	super.onPause();
    }
    
    @Override
    protected void onStart() 
    {
    	Log.i(DEBUG_TAG, "OnStart()");
    	super.onStart();
    }
    
    @Override
    protected void onResume() 
    {
    	Log.i(DEBUG_TAG, "OnResume()");
    	super.onResume();
    }
    
    @Override
    protected void onStop() 
    {
    	Log.i(DEBUG_TAG, "OnStop()");
    	super.onStop();
    }
    
    @Override
    protected void onRestart() 
    {
    	Log.i(DEBUG_TAG, "OnRestart()");
    	super.onRestart();
    }
    
    @Override
    protected void onDestroy() 
    {
    	Log.i(DEBUG_TAG, "OnDestroy()");
    	super.onDestroy();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
    	Log.i(DEBUG_TAG, "OnConfigurationChanged()");
    	super.onConfigurationChanged(newConfig);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
//    	if(keyCode == KeyEvent.KEYCODE_BACK )
 //   		return false;
    	
    	return super.onKeyDown(keyCode, event);
    }

    
    private void setupViews()
    {
        mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
        Button button1  = (Button) findViewById(R.id.Button01);
        Button button2 = (Button) findViewById(R.id.Button02);
        mEditText = (EditText) findViewById(R.id.EditText01);
        mListView = (ListView) findViewById(android.R.id.list);
        
        mContainer = (ViewGroup) findViewById(R.id.container);
        mTextView = (TextView) findViewById(R.id.TextView04);
        
        mTextView.setClickable( true );
        mTextView.setFocusable( true);
        
        mListView.setOnItemClickListener( new AdapterView.OnItemClickListener() 
        {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id)
			{
				mViewFlipper.clearAnimation();
				mTextView.setText("sunjae");
				applyRotation(position, 0, 180);
			}
		});
        
        mTextView.setOnClickListener( new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				mViewFlipper.clearAnimation();
				 applyRotation(-1, 180, 0);
			}
		});
        
       mViewFlipper.setPersistentDrawingCache( ViewGroup.PERSISTENT_ANIMATION_CACHE);
        
        button1.setOnClickListener( new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				mViewFlipper.setInAnimation(inFromRightAnimation());
				mViewFlipper.setOutAnimation(outToLeftAnimation());
				mViewFlipper.setDisplayedChild(1);
				
			}
		});
        
        button2.setOnClickListener(new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				mViewFlipper.setInAnimation(inFromLeftAnimation());
				mViewFlipper.setOutAnimation(outToRightAnimation());
				mViewFlipper.setDisplayedChild(0);
			}
		});
        
        mEditText.setOnClickListener( new View.OnClickListener() 
        {
			@Override
			public void onClick(View v) 
			{
				sendText();
			}
		});
        
        mEditText.setOnKeyListener( new View.OnKeyListener() 
        {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) 
			{
				if(event.getAction() == KeyEvent.ACTION_DOWN)
				{
					switch (keyCode) 
					{
						case KeyEvent.KEYCODE_ENTER:
						{
							sendText();
							return true;
						}
					}
				}
				return false;
			}
		});
    }
    

    
    
    private Animation inFromRightAnimation()
    {
		Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f, 
					Animation.RELATIVE_TO_PARENT, 0.0f
					);
		
		inFromRight.setDuration(FLIP_VELOCITY);
		
		inFromRight.setInterpolator(new OvershootInterpolator());
		
		return inFromRight;

    }
    
    private Animation outToLeftAnimation()
    {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		
		outtoLeft.setDuration(FLIP_VELOCITY);
		
		outtoLeft.setInterpolator(new OvershootInterpolator());
		
		return outtoLeft;
    }
    
    private Animation inFromLeftAnimation()
    {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		inFromLeft.setDuration(FLIP_VELOCITY);
		
		inFromLeft.setInterpolator(new OvershootInterpolator());
		
		return inFromLeft;
    }
    
    private Animation outToRightAnimation() 
    {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f
				);
		
		outtoRight.setDuration(FLIP_VELOCITY);
		
		outtoRight.setInterpolator(new OvershootInterpolator());
		
		return outtoRight;
	}
    
    private void applyRotation(int position, float start, float end) 
    {
        // Find the center of the container
        final float centerX = mViewFlipper.getWidth() / 2.0f;
        final float centerY = mViewFlipper.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(position));

        mViewFlipper.startAnimation(rotation);
    }
    
    private final class DisplayNextView implements Animation.AnimationListener 
    {
        private final int mPosition;

        private DisplayNextView(int position) 
        {
            mPosition = position;
        }

        public void onAnimationStart(Animation animation) 
        {
        }

        public void onAnimationEnd(Animation animation)
        {
        	mViewFlipper.post(new SwapViews(mPosition));
        }

        public void onAnimationRepeat(Animation animation) 
        {
        }
    }
    
    private final class SwapViews implements Runnable
    {
        private final int mPosition;

        public SwapViews(int position) 
        {
            mPosition = position;
        }

        public void run() 
        {
            final float centerX = mViewFlipper.getWidth() / 2.0f;
            final float centerY = mViewFlipper.getHeight() / 2.0f;
            Rotate3dAnimation rotation;
            
            if (mPosition > -1) 
            {
            	mTextView.setTextSize(100);
            	mViewFlipper.setDisplayedChild(2);
           	
                rotation = new Rotate3dAnimation(0, 0, centerX, centerY, 310.0f, false);
            } 
            else 
            {
            	mViewFlipper.setDisplayedChild(0);
                rotation = new Rotate3dAnimation(0, 0, centerX, centerY, 310.0f, false);
            }

            rotation.setDuration(10);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());

            mViewFlipper.startAnimation(rotation);
        }
    }
   
    
}